const fetch = require('../../utils/fetch.js')
const weatherMap = {
  '晴': 'sunny',
  '多云': 'cloudy',
  '阴': 'overcast',
  '小雨': 'lightrain',
  '大雨': 'heavyrain',
  '阵雨': 'shower',
  '雪': 'snow',
  '雪加雨': 'sleet'
}
const weathercolorMap = {
  'sunny': '#4AB0FD',
  'cloudy': '#2F3291',
  'overcast': '#4282a0',
  'lightrain': '#A4DBE7',
  'heavyrain': '#E7D977',
  'shower': '#5E5C56',
  'snow': '#3DB8B2',
  'sleet': '#B6C0C0'
}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    item: 0,
    tab:0,
    motto:'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    temp: "4",
    temperature: "-1/10C",
    weather: "晴",
    city: "北京",
    date: "2000-01-01",
    direction: "无持续风向微风级",
    condition: '10',
    futureArray: '[]',
    weatherImage: 'sunny',
    array: ["1", "2", "3"]
  },
changeItem: function(e){
this.setData({
  item:e.target.dataset.item
})
},
changeTab:function(e){
this.setData({
  tab: e.detail.current
})
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({ title: "努力加载中" })
    //请求数据
    fetch('food/index').then(res => {
      //请求成功，关闭对话框
      wx.hideLoading();
      //把接口返回数据setData给listData
      this.setData({ listData: res.data })
    }, () => {
      //请求失败，关闭对话框，执行fetch. js文件中的fail方法
      wx.hideLoading()
    })
    this.getweatherData()
  },
  getweatherData(callback) {
    wx.request({
      url: 'http://apis.juhe.cn/simpleWeather/query',
      data: {
        format: 2,
        city: '成都',
        key: '20bfc2f4b32eb6162620082c82ce7921'
      },
      success: res => {
        this.getNow(res)
      },
      complete: () => {
        callback && callback()
      }
    })
  },
  getNow(res) {
    console.log(res)
    let nowtemp = res.data.result.realtime.temperature
    let nowweather = res.data.result.future[0].weather
    let city = res.data.result.city 
    let temperature = res.data.result.future[0].temperature
    let direction = res.data.result.future[0].direct
    let date = res.data.result.future[0].date
    let futureArray = res.data.result.future
    let weatherImage = weatherMap[imageName]
    var imageName = ''
    let endIndex = nowweather.indexOf('转')
    if (endIndex >= 0) {
      imageName = nowweather.substring(0, endIndex)
    } else {
      imageName = nowweather
    }
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: weathercolorMap[weatherMap[imageName]],
    })
    this.setData({
      temp: nowtemp,
      temperature: temperature,
      weather: nowweather,
      city: city,
      date: date,
      direction: direction,
      futureArray: futureArray,
      weatherImage: weatherMap[imageName]
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log("page onReady")
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log("page onShow")
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})